package com.meihua.baoleyi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.meihua.baoleyi.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * Created by liming on 2017/10/17
 */

public class PayPwdResetDialog extends Dialog {
    @Bind(R.id.tv_dlg_msg)
    TextView tvMsg;
    @Bind(R.id.tv_dlg_yes)
    TextView tvYes;
    @Bind(R.id.tv_dlg_no)
    TextView tvNo;
    @Bind(R.id.tv_dlg)
    TextView tv_dlg;

    private Context mContext;
    private String mMessage;
    private String mPhone;
    private String mfanli;
    private String mPositiveButtonName;
    private OnClickListener mPositiveListener;
    private String mNativeButtonName;
    private OnClickListener mNativeListener;

    public PayPwdResetDialog(Context context,
                             String message,
                             String phone,
                             String fanli,
                             String positiveButtonName,
                             OnClickListener positiveListener,
                             String nativeButtonName,
                             OnClickListener nativeListener) {
        super(context, R.style.dialog);
        mContext = context;
        mMessage = message;
        mPhone = phone;
        mfanli = fanli;
        mPositiveButtonName = positiveButtonName;
        mPositiveListener = positiveListener;
        mNativeButtonName = nativeButtonName;
        mNativeListener = nativeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_reset_pay_pws);
        ButterKnife.bind(this);
        settings();
    }

    private void settings() {
        if (mfanli.equals("fanli")) {
            setfanli(mfanli,mPhone);
        } else if (mfanli.equals("contacts")){
            setContacts(mPhone);
        }else {
            setPhone(mPhone);
        }
        setPositiveButton();
        setNativeButton();
    }

    private void setNativeButton() {

        if (!TextUtils.isEmpty(mNativeButtonName)) {
            tvNo.setText(mNativeButtonName);
        }
    }

    private void setPositiveButton() {

        if (!TextUtils.isEmpty(mPositiveButtonName)) {
            tvYes.setText(mPositiveButtonName);
        }
    }

    @OnClick({R.id.tv_dlg_yes, R.id.tv_dlg_no})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.tv_dlg_yes:
                if (mPositiveListener != null) {
                    mPositiveListener.onClick(PayPwdResetDialog.this, v.getId());
                }
                break;
            case R.id.tv_dlg_no:
                if (mNativeListener != null) {
                    mNativeListener.onClick(PayPwdResetDialog.this, v.getId());
                }
                break;
        }
    }


    public void setPhone(String phone) {
        String msg = "您是否记得账号【手机号码】\n当前使用的支付密码";
        phone = phone.replace(phone.substring(3, 7), "****");
        msg = msg.replace("【手机号码】", phone);
        tvMsg.setText(msg);
        tv_dlg.setVisibility(View.GONE);
    }
    public void setContacts(String phone) {
        tv_dlg.setVisibility(View.VISIBLE);
        tvMsg.setText("您是否要保存手机号:");
        tv_dlg.setTextColor(0xff171717);
        tv_dlg.setTextSize(24);
        tv_dlg.setText(phone+"到通讯录");
    }

    public void setfanli(String fanli,String phone) {
        tv_dlg.setVisibility(View.VISIBLE);
        String msg = "恭喜您领取"+phone+"个积分!";
        tvMsg.setText(msg);
        tv_dlg.setText("(领取积分后将结束本单返利)");

    }

    public static class Builder {
        private Context mContext;
        private String message;
        private String phone;
        private String mPositiveButtonName;
        private OnClickListener mPositiveListener;
        private String mNativeButtonName;
        private OnClickListener mNativeListener;
        private String fanli;

        public Builder(Context context) {
            mContext = context;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public void setfanli(String fanli) {
            this.fanli = fanli;
        }

        public void setPositiveButton(String s, OnClickListener l) {
            mPositiveButtonName = s;
            mPositiveListener = l;
        }

        public void setNativeButton(String s, OnClickListener l) {
            mNativeButtonName = s;
            mNativeListener = l;
        }

        public PayPwdResetDialog build() {
            PayPwdResetDialog dlg = new PayPwdResetDialog(
                    mContext,
                    message,
                    phone,
                    fanli,
                    mPositiveButtonName,
                    mPositiveListener,
                    mNativeButtonName,
                    mNativeListener);
            return dlg;
        }
    }
}
